package com.szbber.medical.board.ui.fragment

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager.widget.ViewPager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.flyco.tablayout.listener.OnTabSelectListener
import com.szbber.commen.base.BaseFragmentPageAdapter
import com.szbber.commen.base.BaseVMFragment
import com.szbber.commen.bean.ScheduleBean
import com.szbber.commen.dialog.DialogUtils
import com.szbber.medical.board.R
import com.szbber.medical.board.databinding.BoardFragmentScheduleBinding
import com.szbber.medical.board.databinding.BoardItemMessageBinding
import com.szbber.medical.board.databinding.BoardItemScheduleBinding
import com.szbber.medical.board.viewModel.ScheduleFragmentViewModel
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import kotlinx.android.synthetic.main.board_activity_home.*
import kotlinx.android.synthetic.main.board_fragment_schedule.*
import kotlinx.android.synthetic.main.board_fragment_schedule.mViewPager

class ScheduleFragment : BaseVMFragment<BoardFragmentScheduleBinding, ScheduleFragmentViewModel>() {

    companion object {
        fun getInstance(): ScheduleFragment {
            return ScheduleFragment()
        }
    }

    private var myAdapter: BaseFragmentPageAdapter? = null
    private val mTitleList = ArrayList<String>()
    private val mFragments: ArrayList<Fragment> = ArrayList()
    override fun getLayoutResId(): Int = R.layout.board_fragment_schedule

    override fun initVM(): ScheduleFragmentViewModel =
        ViewModelProvider(this, ViewModelProvider.NewInstanceFactory()).get(
            ScheduleFragmentViewModel::class.java)

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.scheduleVm = mViewModel

        mViewModel.getDeptName()
        mViewModel.getSchedule(activity)
        mViewModel.scheduleBean.observe(this) {
            observeRecyclerView(it)
        }
    }

    private fun observeRecyclerView(data: ScheduleBean) {

        val adapter = object :
            BaseQuickAdapter<ScheduleBean.Bed, BaseViewHolder>(R.layout.board_item_schedule,
                data.data.bedList.toMutableList()) {
            override fun convert(holder: BaseViewHolder, item: ScheduleBean.Bed) {
                val binding =
                    DataBindingUtil.bind<ViewDataBinding>(holder.itemView) as BoardItemScheduleBinding
                binding.schedulVM = item
                if (holder.layoutPosition%2==0){
                    binding.rlItem.setBackgroundColor(context.resources.getColor(R.color.white))
                }else{
                    binding.rlItem.setBackgroundColor(context.resources.getColor(R.color.color_efefef))
                }
                val adapter = object : TagAdapter<ScheduleBean.BedCode>(item.bedCode){
                    @SuppressLint("SetTextI18n")
                    override fun getView(parent: FlowLayout?,
                        position: Int,
                        t: ScheduleBean.BedCode?): View {
                        val view =View.inflate(context, R.layout.board_item_schedule_clid, null)
                        val tvName = view.findViewById<TextView>(R.id.tvName)
                        val cvItem = view.findViewById<CardView>(R.id.cvItem)
                        t?.bedCode?.apply {
                            cvItem.visibility= View.VISIBLE
                            tvName.text = "${this}床"
                            tvName.setOnClickListener {
                                DialogUtils.showDeleteDialog(context){
                                    mViewModel.deleteCareLevel(context,t.id.toString() )
                                }
                            }
                        }
                        return  view
                    }
                }
               binding.mFlowLayout.adapter = adapter
                binding.cvItem.setOnClickListener {
                    mViewModel.getPatientList(context, item.careLabelId.toString())
                }
            }
        }
        mRecyclerView.adapter = adapter
        val manager = LinearLayoutManager(context)
        mRecyclerView.layoutManager = manager
        initOperationLayout(data)
        initMessageLayout(data)
    }

    var arr = mutableListOf<String>("#b3e5fc", "#b39ddb","#ce93d8",
        "#f48fb1","#80cbc4", "#ffcc80" ,"#c5e1a5","#80cbc4")
    var iconarr = mutableListOf<Int>(
        R.drawable.icon_operation1,R.drawable.icon_operation2,R.drawable.icon_operation3,
        R.drawable.icon_operation4,R.drawable.icon_operation5,R.drawable.icon_operation6,
    )

    private  fun initMessageLayout(data:ScheduleBean){
        val adapter = object :BaseQuickAdapter<ScheduleBean.MessageVo, BaseViewHolder>(
            R.layout.board_item_message, data.data.messageVos.toMutableList()
        ){
            override fun convert(holder: BaseViewHolder, item: ScheduleBean.MessageVo) {
                val binding =
                    DataBindingUtil.bind<ViewDataBinding>(holder.itemView) as BoardItemMessageBinding
                binding.messageVm = item
                binding.llItem.setBackgroundColor(Color.parseColor(arr[(0 until arr.size).random()]))
                binding.ivIcon.setBackgroundResource(iconarr[(0 until iconarr.size).random()])
            }
        }

        mMessage.adapter = adapter
        mMessage.layoutManager = LinearLayoutManager(activity)

    }

  private  fun initOperationLayout(data:ScheduleBean){
        mTitleList.clear()
        mTitleList.add("今日手术")
        mTitleList.add("明日手术")
        mFragments.clear()
        mFragments.add(TodayOperationFragment.getInstance(data.data.operationVos))
        mFragments.add(TodayOperationFragment.getInstance(data.data.operationVos))

        myAdapter = BaseFragmentPageAdapter(childFragmentManager, mFragments, mTitleList)
        mViewPager.adapter = myAdapter
        mViewPager.currentItem = 0
        mCommonTabLayout.setViewPager(mViewPager)
        mCommonTabLayout.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                mViewPager.currentItem = position
            }
            override fun onTabReselect(position: Int) {}
        })

        mViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                mCommonTabLayout.currentTab = position
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })

    }

}